package aad.storyboard.model;

import android.graphics.Rect;

public class ParticleAliveState implements ParticleState{

	 Particle particle;
		
		public ParticleAliveState(Particle particle)
		{
			this.particle=particle;
		}
	
	
	@Override
	public void update() {
		particle.posX += particle.getXv();
		particle.posY += particle.getYv();
		
		// extract alpha
		int a = particle.getColor() >>> 24;
		a -= 5;								// fade by 5
		if (a <= 0) {						// if reached transparency kill the particle
			particle.setNewState(particle.getStateDead());
		} else {
			particle.setColor((particle.getColor() & 0x00ffffff) + (a << 24));		// set the new alpha
			particle.getPaint().setAlpha(a);
			particle.setAge(particle.getAge() + 1);				// increase the age of the particle

		}
		if (particle.getAge() >= particle.getLifetime()) {	// reached the end if its life
			particle.setNewState(particle.getStateDead());
		}
		
	}


	@Override
	public void update(Rect container) {
		if (particle.posX <= container.left || particle.posX >= container.right - particle.getWidth()) {
			particle.setXv(particle.getXv() * -1);
		}
		// Bottom is 480 and top is 0 !!!
		if (particle.posY <= container.top || particle.posY >= container.bottom - particle.getHeight()) {
			particle.setYv(particle.getYv() * -1);
		}
		
	}


	@Override
	public void reset(float x, float y) {
		// TODO Auto-generated method stub
		
	}
	

}
